package com.cn.yygh.hosp.controller.api;

import org.springframework.data.domain.Page;
import com.cn.yygh.common.exception.YyghException;
import com.cn.yygh.common.helper.HttpRequestHelper;
import com.cn.yygh.common.result.Result;
import com.cn.yygh.common.result.ResultCodeEnum;
import com.cn.yygh.common.utils.MD5;
import com.cn.yygh.hosp.service.DepartmentService;
import com.cn.yygh.hosp.service.HospitalService;
import com.cn.yygh.hosp.service.HospitalSetService;
import com.cn.yygh.model.hosp.Department;
import com.cn.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*
科室列表
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiDepartmentController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号和科室编号
        String hoscode=(String)paramMap.get("hoscode");
        String depcode=(String)paramMap.get("depcode");
        //todo 签名校验
        departmentService.removeDepartment(hoscode,depcode);

        return Result.ok();

    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode=(String)paramMap.get("hoscode");

        //当前页 和 每页记录数
        //判断是否为空，为空默认值为1，不为空 取到值转为int类型
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //todo 签名校验
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //
        Page<Department> pageModel =departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取传来的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //1. 获取传递过来的签名，进行MD5加密  ;此时获得的签名是已加密的，HttpRequestHelper工具类中有Md5加密
        String hospSign = (String) paramMap.get("sign");
        //2. 根据传递过来的 医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3. 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4. 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }
}
